// pages/score/index.js
import urls from '../../utils/urls.js';
import loginFn from '../../utils/login.js';
var app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		list: [],
		schoolScore: 0,
		serviceScore: 0,
		effectScore: 0,
		anonymous: false,
		imgs: [],
		hasSave: false,
		comedetail: false
	},
	saveImg: [], //保存图片id
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		if (options.list) {
			var list = []
			list.push(JSON.parse(options.list))
			this.setData({
				list: list
			})
		};
		if (options.courseId) {
			var pages = getCurrentPages();
			var prevPage = pages[pages.length - 2];
			var datas = []
			datas.push(prevPage.data.alldetail)
			console.log(datas)
			this.setData({
				list: datas,
				comedetail: true
			})
		};

	},
	textcontent: '', //评论内容
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	//评分
	onschoolChange: function(e) {
		this.setData({
			schoolScore: e.detail.value
		})
	},
	onteacherChange: function(e) {
		this.setData({
			serviceScore: e.detail.value
		})
	},
	onclassChange: function(e) {
		this.setData({
			effectScore: e.detail.value
		})
	},
	//匿名
	radioChange(e) {
		var anonymous = !this.data.anonymous
		this.setData({
			anonymous: anonymous
		})
	},
	//保存评论
	saveFn: function() {
		this.setData({
			hasSave: true
		})
		var that = this;
		var loginInfo = loginFn()
		loginInfo.remark = this.textcontent
		loginInfo.courseId = this.data.list[0].courseId
		loginInfo.orderId = this.data.list[0].orderId
		loginInfo.schoolScore = this.data.schoolScore
		loginInfo.serviceScore = this.data.serviceScore
		loginInfo.effectScore = this.data.effectScore
		loginInfo.images = this.saveImg
		if (this.data.anonymous) {
			loginInfo.anonymous = 1
		} else {
			loginInfo.anonymous = 0
		}
		wx.request({
			url: urls.saveCourse,
			method: "POST",
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					wx.showToast({
						title: '评论成功',
					})
					setTimeout(function() {
						wx.navigateBack({
							delta: 1
						})
					}, 2000)

				} else {
					wx.showToast({
						title: res.data.info,
					})
					that.setData({
						hasSave: false
					})
				};
			}
		})
	},
	//取消评论
	cancelFn: function() {
		wx.navigateBack({
			delta: 1
		})
	},
	//评论内容
	textareafn: function(e) {
		this.textcontent = e.detail.value
	},
	//上传图片
	uploadImg: function() {
		var that = this;
		wx.chooseImage({
			count: 9,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				// tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths
				for (var i = 0; i < tempFilePaths.length; i++) {
					that.uploadFn(tempFilePaths[i])
				}
			}
		})
	},
	uploadFn(tempFilePaths) {
		var that = this;
		app.uploadImage(tempFilePaths, (res) => {
			if(res.code == 1) {
				var saveImg = that.saveImg;
				saveImg.push(res.data.imageFileId)
				that.saveImg = saveImg
				var insImgs = that.data.imgs;
				insImgs.push(tempFilePaths)
				that.setData({
					imgs: insImgs,
					saveImg: saveImg
				})
			} else {
				app.alert(res.msg);
			}
		})
	},
	closeimg(e) { //删除图片
		var imgs = this.data.imgs
		imgs.splice(e.currentTarget.dataset.idx, 1)
		var saveimg = this.saveImg
		saveimg.splice(e.currentTarget.dataset.idx, 1)
		this.setData({
			imgs: imgs
		})
	}
})
